﻿using CaseManagerLibrary.Models;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SmartLight
{
    public partial class FrmSmartLight : Form
    {
        private SmartLightManager smartLightManager;
        private SmartLightDevice currDevice;
        private List<SmartLightDevice> DeviceList;

        public FrmSmartLight()
        {
            InitializeComponent();

            Init();
        }

        private void Init()
        {
            smartLightManager = new SmartLightManager();
            smartLightManager.AudioList.Add(new AudioInfo() { id = 1, name = "B01对象行为异常", time = 2 });
            smartLightManager.AudioList.Add(new AudioInfo() { id = 2, name = "B01开始谈话", time = 2 });
            smartLightManager.AudioList.Add(new AudioInfo() { id = 3, name = "B01谈话结束", time = 2 });
            smartLightManager.AudioList.Add(new AudioInfo() { id = 4, name = "B02对象行为异常", time = 2 });

            DeviceList = new List<SmartLightDevice>();

            foreach (string key in System.Configuration.ConfigurationManager.AppSettings.AllKeys)
            {
                string val = System.Configuration.ConfigurationManager.AppSettings[key];

                var device = new SmartLightDevice();
                device.id = key.Split("#".ToCharArray())[0];
                device.name = key.Split("#".ToCharArray())[1];
                device.ip = val.Split(":".ToCharArray())[0];
                device.port = Convert.ToInt32(val.Split(":".ToCharArray())[1]);

                DeviceList.Add(device);
            }

            cmbDeviceIP.DataSource = DeviceList;
            cmbDeviceIP.DisplayMember = "ip";

            if (cmbDeviceIP.Items.Count > 0)
            {
                cmbDeviceIP.SelectedIndex = 0;
                currDevice = DeviceList[cmbDeviceIP.SelectedIndex];
            }

            for (int i = 1; i <= 30; i++)
            {
                cmbVolume.Items.Add(i);
            }

            cmbColor.SelectedIndex = 0;
            cmbLightType.SelectedIndex = 0;
            cmbSound.SelectedIndex = 0;
            cmbSoundLoop.SelectedIndex = 0;

            btnDisconnect.Enabled = false;
            btnCmd.Enabled = false;
            btnBatch.Enabled = false;
            cmbVolume.Enabled = false;
        }


        private void btnConnect_Click(object sender, EventArgs e)
        {
            var ret = smartLightManager.Connect(currDevice.ip, currDevice.port);

            if (ret.success)
            {
                MessageBox.Show(ret.message);

                btnConnect.Enabled = false;
                btnDisconnect.Enabled = true;
                btnCmd.Enabled = true;
                btnBatch.Enabled = true;
                cmbVolume.Enabled = true;

                int vol = smartLightManager.GetVolume();

                cmbVolume.SelectedIndex = vol - 1;
            }

        }

        private void btnCmd_Click(object sender, EventArgs e)
        {
            LightColor color = (LightColor)Enum.ToObject(typeof(LightColor), cmbColor.SelectedIndex + 1);
            LightType lightType = (LightType)Enum.ToObject(typeof(LightType), cmbLightType.SelectedIndex);
            AudioInfo audio = smartLightManager.GetAudio(cmbSound.SelectedIndex);
            SoundPlay soundPlay = (SoundPlay)Enum.ToObject(typeof(SoundPlay), cmbSoundLoop.SelectedIndex);

            HResult ret;

            if (audio == null)
            {
                if (lightType == LightType.OFF)
                {
                    ret = smartLightManager.LightOff();
                }
                else
                {
                    ret = smartLightManager.LightWithOutSound(color, lightType);
                }
            }
            else
            {
                if (soundPlay == SoundPlay.STOP)
                {
                    ret = smartLightManager.StopSound();
                }
                else
                {
                    if (lightType == LightType.OFF)
                    {
                        ret = smartLightManager.LightOff();
                    }
                    else
                    {
                        ret = smartLightManager.LightWithSound(color, lightType, audio.id, soundPlay);
                    }
                }
            }

            txtCmd.AppendText(ret.message);
            txtCmd.AppendText("\r\n");

        }

        private void btnClearLog_Click(object sender, EventArgs e)
        {
            txtCmd.Clear();
        }

        private void cmbDeviceIP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDeviceIP.SelectedIndex >= 0)
            {
                txtDevicePort.Text = DeviceList[cmbDeviceIP.SelectedIndex].port.ToString();
                currDevice = DeviceList[cmbDeviceIP.SelectedIndex];
            }
        }

        private void BtnDisconnect_Click(object sender, EventArgs e)
        {
            smartLightManager.Close();

            btnConnect.Enabled = true;
            btnDisconnect.Enabled = false;
            btnCmd.Enabled = false;
            btnBatch.Enabled = false;
            cmbVolume.Enabled = false;

            MessageBox.Show("device disconnected");

        }

        /// <summary>
        /// 生成语音播报.mp3文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSound_Click(object sender, EventArgs e)
        {
            var speech = new SpeechSynthesizer();
            var dialog = new SaveFileDialog();
            dialog.Filter = "*.mp3 | *.mp3";
            dialog.ShowDialog();

            string path = dialog.FileName;
            if (path.Trim().Length == 0)
            {
                return;
            }
            speech.SetOutputToWaveFile(path);
            speech.Volume = 100;
            speech.Rate = 0;
            speech.Speak(txtCmd.Text);
            speech.SetOutputToNull();
            MessageBox.Show("生成成功!在" + path + "路径中！", "提示");
        }

        private void btnBatch_Click(object sender, EventArgs e)
        {
            switch (cmbBatch.SelectedIndex)
            {
                case 0:
                    BatchCmd1();
                    break;
                case 1:
                    BatchCmd2();
                    break;
                case 2:
                    BatchCmd3();
                    break;
                case 3:
                    BatchCmd4();
                    break;
                case 4:
                    BatchCmd5();
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 批处理1：红灯慢闪，语音播报三次”B01对象行为异常“，之后红灯常亮
        /// </summary>
        private void BatchCmd1()
        {
            AudioInfo audio = smartLightManager.GetAudio("B01对象行为异常");

            smartLightManager.LightWithSound(LightColor.RED, LightType.SLOW_FLASH, audio.id, SoundPlay.ONCE, audio.time + 1);

            smartLightManager.LightWithSound(LightColor.RED, LightType.SLOW_FLASH, audio.id, SoundPlay.ONCE, audio.time + 1);

            smartLightManager.LightWithSound(LightColor.RED, LightType.SLOW_FLASH, audio.id, SoundPlay.ONCE, audio.time + 1);

            smartLightManager.LightWithOutSound(LightColor.RED, LightType.NORMAL);

        }

        /// <summary>
        /// 批处理2：红灯慢闪，语音循环播报“B01对象行为异常”
        /// </summary>
        private void BatchCmd2()
        {
            AudioInfo audio = smartLightManager.GetAudio("B01对象行为异常");

            smartLightManager.LightWithSound(LightColor.RED, LightType.SLOW_FLASH, audio.id, SoundPlay.LOOP);
        }

        /// <summary>
        /// 批处理3：黄灯慢闪，语音播报三次“B01开始谈话”，之后黄灯常亮三秒熄灭
        /// </summary>
        private void BatchCmd3()
        {
            AudioInfo audio = smartLightManager.GetAudio("B01开始谈话");

            smartLightManager.LightWithSound(LightColor.YELLOW, LightType.SLOW_FLASH, audio.id, SoundPlay.ONCE, audio.time + 1);

            smartLightManager.LightWithSound(LightColor.YELLOW, LightType.SLOW_FLASH, audio.id, SoundPlay.ONCE, audio.time + 1);

            smartLightManager.LightWithSound(LightColor.YELLOW, LightType.SLOW_FLASH, audio.id, SoundPlay.ONCE, audio.time + 1);

            smartLightManager.LightWithOutSound(LightColor.YELLOW, LightType.NORMAL, 3);

            smartLightManager.LightOff();

        }

        /// <summary>
        /// 批处理4：绿灯慢闪，语音播报三次“B01谈话结束”，之后绿灯常亮三秒熄灭
        /// </summary>
        private void BatchCmd4()
        {
            AudioInfo audio = smartLightManager.GetAudio("B01谈话结束");

            smartLightManager.LightWithSound(LightColor.GREEN, LightType.SLOW_FLASH, audio.id, SoundPlay.ONCE, audio.time + 1);

            smartLightManager.LightWithSound(LightColor.GREEN, LightType.SLOW_FLASH, audio.id, SoundPlay.ONCE, audio.time + 1);

            smartLightManager.LightWithSound(LightColor.GREEN, LightType.SLOW_FLASH, audio.id, SoundPlay.ONCE, audio.time + 1);

            smartLightManager.LightWithOutSound(LightColor.GREEN, LightType.NORMAL, 3);

            smartLightManager.LightOff();
        }


        /// <summary>
        ///  批处理5：停止播报并熄灯
        /// </summary>
        private void BatchCmd5()
        {
            smartLightManager.StopSound();
            smartLightManager.LightOff();
        }

        private void cmbVolume_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbVolume.SelectedIndex >= 0)
                smartLightManager.SetVolume(cmbVolume.SelectedIndex + 1);
        }

        private void FrmSmartLight_FormClosing(object sender, FormClosingEventArgs e)
        {
            smartLightManager.Close();
        }
    }
}
